//
//  Tagview.m
//  HandheldELearn
//
//  Created by wsj2015 on 2017/1/8.
//  Copyright © 2017年 陈武琦. All rights reserved.
//

#import "Tagview.h"

@implementation Tagview
{
    UILabel *_tagTitle;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
       
        UIView *blankview = [UIView new];
        self.backgroundColor = [UIColor whiteColor];
        blankview.frame = CGRectMake(0, 0, kScreenWidth, 8);
        blankview.backgroundColor = kTableView_bg;
        [self addSubview:blankview];
        
        UILabel *titleL = [UILabel new];
        titleL.text = [NSString stringWithFormat:@"分类标签:"];
        titleL.textColor =[UIColor grayColor];
        titleL.font = [UIFont systemFontOfSize:14];
        titleL.frame = CGRectMake(20, 20, 100, 20);
        [self addSubview:titleL];
        _tagTitle = titleL;
    }
    return self;
}

- (void)addTags:(NSArray<NSString *> *)tagsArr{
    
    
    CGFloat space = 10;
    UILabel *lastlabel = nil;
    CGFloat y =48 ;
    CGFloat x = 20;
    CGFloat inset = 8;
    CGFloat h = 30;
    for (int i = 0; i < tagsArr.count; i ++) {
        NSString *str = tagsArr[i];
        UILabel *tagLabel = [UILabel new];
        tagLabel.backgroundColor = kTableView_bg;
        tagLabel.layer.cornerRadius = 4;
        tagLabel.layer.masksToBounds = YES;
        tagLabel.layer.borderColor = kBorderColor.CGColor;
        tagLabel.layer.borderWidth = 1;
        tagLabel.text = str;
        tagLabel.textAlignment = NSTextAlignmentCenter;
        tagLabel.textColor = [UIColor grayColor];
        tagLabel.font = [UIFont systemFontOfSize:14];
        CGFloat w = [str boundingRectWithSize:CGSizeMake(400, h) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.width;
        if (lastlabel) {
            if ((CGRectGetMaxX(lastlabel.frame) + space +2*inset + 20 + w) >= kScreenWidth) {
                y = CGRectGetMaxY(lastlabel.frame) + 8;
                x = 20;
            }else{
                x =(space + CGRectGetMaxX(lastlabel.frame));
                NSLog(@"-------%@",@(x));
            }
        }
        tagLabel.frame = CGRectMake(x, y, w + 2 * inset , h);
        lastlabel = tagLabel;
        [self addSubview:lastlabel];
      
    }
    
    
    //自动调整 自己的高度
    
    if (lastlabel) {
        CGRect frame = self.frame;
        frame.size.height = CGRectGetMaxY(lastlabel.frame) + 20;
        self.frame = frame;
    }
   

}


@end
